package com.ruoyi.web.controller.wash;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.wash.domain.CallRecord;
import com.ruoyi.wash.enums.DaoZhaServersEnum;
import com.ruoyi.wash.enums.ThirdSystemInfoEnum;
import com.ruoyi.wash.service.ICallRecordService;
import com.ruoyi.wash.vo.CallRecordDetailVO;
import com.ruoyi.wash.vo.CallRecordReqRespJsonVO;
import com.ruoyi.wash.vo.CallRecordVO;
import com.ruoyi.web.properties.EnvProperties;
import com.ruoyi.web.util.WashModelMapUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 接口调用记录Controller
 *
 * @author YanMing.Wang
 * @date 2024-04-30
 */
@Controller
@RequestMapping("/wash/record")
public class CallRecordController extends BaseController {
    private String prefix = "wash/record";

    @Autowired
    private ICallRecordService iCallRecordService;

    @Autowired
    private EnvProperties envProperties;

    @PreAuthorize("@ss.hasPermi('wash:record:view')")
    @GetMapping()
    public String record(ModelMap modelMap) {
        this.setModelMap(modelMap);
        return prefix + "/record";
    }

    /**
     * 查询接口调用记录列表
     */
    @PreAuthorize("@ss.hasPermi('wash:record:list')")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CallRecord callRecord) {
        startPage();
        List<CallRecordVO> list = iCallRecordService.selectCallRecordList(callRecord);
        return getDataTable(list);
    }

    /**
     * 导出接口调用记录列表
     */
    @PreAuthorize("@ss.hasPermi('wash:record:export')")
    @Log(title = "接口调用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CallRecord callRecord) {
        List<CallRecordVO> list = iCallRecordService.selectCallRecordList(callRecord);
        ExcelUtil<CallRecordVO> util = new ExcelUtil<CallRecordVO>(CallRecordVO.class);
        return util.exportExcel(list, "接口调用记录数据");
    }

    /**
     * 新增接口调用记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存接口调用记录
     */
    @PreAuthorize("@ss.hasPermi('wash:record:add')")
    @Log(title = "接口调用记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CallRecord callRecord) {
        return toAjax(iCallRecordService.insertCallRecord(callRecord));
    }

    /**
     * 修改接口调用记录
     */
    @PreAuthorize("@ss.hasPermi('wash:record:edit')")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        CallRecordVO callRecord = iCallRecordService.selectCallRecordByRecordId(recordId);
        mmap.put("callRecord", callRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存接口调用记录
     */
    @PreAuthorize("@ss.hasPermi('wash:record:edit')")
    @Log(title = "接口调用记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CallRecord callRecord) {
        return toAjax(iCallRecordService.updateCallRecord(callRecord));
    }

    /**
     * 接口调用记录详情
     */
    @GetMapping("/detail/{recordId}")
    public String detail(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        CallRecordDetailVO detailVO = new CallRecordDetailVO();
        CallRecordVO callRecord = iCallRecordService.selectCallRecordByRecordId(recordId);
        BeanUtils.copyProperties(callRecord, detailVO);
        List<CallRecordVO> callRecords = iCallRecordService.selectRecordsByLinkId(callRecord.getLinkId());
        List<CallRecordReqRespJsonVO> jsonVOS = null;
        if (callRecords != null && callRecords.size() > 0) {
            jsonVOS = new ArrayList<>();
            for (CallRecordVO tmp : callRecords) {
                CallRecordReqRespJsonVO respJsonVO = new CallRecordReqRespJsonVO();
                respJsonVO.setRecordName(tmp.getRecordName());
                respJsonVO.setServerEn(tmp.getServerEn());
                respJsonVO.setServerCn(tmp.getServerCn());
                respJsonVO.setRequestJson(tmp.getRequestJson());
                respJsonVO.setResponseJson(tmp.getResponseJson());
                jsonVOS.add(respJsonVO);
            }
        }
        detailVO.setJsonVOS(jsonVOS);
        mmap.put("callRecord", detailVO);
        return prefix + "/detail";
    }

    /**
     * 删除接口调用记录
     */
    @PreAuthorize("@ss.hasPermi('wash:record:remove')")
    @Log(title = "接口调用记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(iCallRecordService.deleteCallRecordByRecordIds(ids));
    }

    /**
     * set
     *
     * @param modelMap
     */
    private void setModelMap(ModelMap modelMap) {
        if (modelMap == null) {
            modelMap = new ModelMap();
        }
        WashModelMapUtil.setEnvirons(modelMap);
        WashModelMapUtil.setThirdSystems(modelMap);
        WashModelMapUtil.setServersDZ(modelMap);
        WashModelMapUtil.setServersLTF(modelMap);
        WashModelMapUtil.setCallStatusList(modelMap);
        // 下拉框默认值
        modelMap.put("environEn", this.getEnvType());
        modelMap.put("thirdEn", ThirdSystemInfoEnum.DAO_ZHA.getCode());
        modelMap.put("serverEn", DaoZhaServersEnum.SWAGGER_VEHICLE_IN.getCode());
    }

    /**
     * 获取环境
     *
     * @return
     */
    public String getEnvType() {
        String active = envProperties.getActive();
        String[] actives = active.split(",");
        return actives[1];
    }

}
